package cn.ldf.keepaccounts.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import cn.ldf.keepaccounts.entity.vo.R;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 异常处理类
 *
 * @author ldf
 * @code May there be no bugs in the world!
 */
@Slf4j
@ControllerAdvice
public class HandlerException {

    @ResponseBody
    @ExceptionHandler(NotLoginException.class)
    public R<?> notLoginException(NotLoginException e){
        String id = RandomUtil.randomString(8).toUpperCase();
        log.error("错误编码:{}", id);
        log.error("服务器异常e:{}",e.getMessage());
        return R.fail(401,e.getMessage());
    }
    @ResponseBody
    @ExceptionHandler(ServerException.class)
    public R<?> serverException(ServerException e){
        String id = RandomUtil.randomString(8).toUpperCase();
        log.error("错误编码:{}", id);
        log.error("服务器异常",e);
        return R.fail(500,e.getMessage());
    }
    // 处理 @Valid 校验异常
    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<?> serverException(MethodArgumentNotValidException e){
        String id = RandomUtil.randomString(8).toUpperCase();
        log.error("错误编码:{}", id);
        log.error("服务器异常",e);
        List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
        String message = allErrors.stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining(StrPool.COMMA));
        return R.fail(500,message);
    }

    // 处理 @Validated 校验异常
    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public R<?> handleConstraintViolationException(ConstraintViolationException e) {
        String id = RandomUtil.randomString(8).toUpperCase();
        log.error("错误编码:{}", id);
        log.error("服务器异常",e);
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        String message = violations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(StrPool.COMMA));
        return R.fail(500,message);
    }
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public R<?> exception(Exception e){
        String id = RandomUtil.randomString(8).toUpperCase();
        log.error("错误编码:{}", id);
        log.error("服务器异常",e);
        return R.fail(500,e.getMessage());
    }
}
